package com.example.flutter_bdface_plugin;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.flutter.plugin.common.PluginRegistry;
import java.util.HashMap;
import java.util.Map;

/** FlutterBdfacePlugin */
public class FlutterBdfacePlugin  implements FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.ActivityResultListener{
  private static Activity activity;
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private static Context mContext;
  private static final String CHANNEL_METHOD= "flutter_bdface_plugin";
  private FlutterBdfacePlugin.LivenessCallback livenessCallback;
  private FlutterBdfacePlugin.DetectCallback detectCallback;

  public static void registerWith(Registrar registrar) {
    activity = registrar.activity();
    mContext = registrar.context();
    final FlutterBdfacePlugin plugin = new FlutterBdfacePlugin();
    final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL_METHOD);
    channel.setMethodCallHandler(plugin);
    registrar.addActivityResultListener(plugin);
  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    //activity = flutterPluginBinding.getApplicationContext();
    mContext = flutterPluginBinding.getApplicationContext();
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_bdface_plugin");
    channel.setMethodCallHandler(this);
    EventBus.getDefault().register(this);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
    EventBus.getDefault().unregister(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if ("liveness".equals(call.method)) {
      livenessCallback = new FlutterBdfacePlugin.LivenessCallback(result);
      liveness(call.hasArgument("language") ? call.<String>argument("language") : null);
    } else if ("detect".equals(call.method)) {
      detectCallback = new FlutterBdfacePlugin.DetectCallback(result);
      detect(call.hasArgument("language") ? call.<String>argument("language") : null);
    }
  }

  private void detect(String language) {
    Intent intent = new Intent(activity, FaceDetectExpActivity.class);
    Bundle mBundle = new Bundle();
    mBundle.putString("language", (language == null || "".equals(language)) ? "zh" : language);
    intent.putExtras(mBundle);
    activity.startActivity(intent);
  }

  private void liveness(String language) {
    Intent intent = new Intent(activity, FaceLivenessExpActivity.class);
    Bundle mBundle = new Bundle();
    mBundle.putString("language", (language == null || "".equals(language)) ? "zh" : language);
    intent.putExtras(mBundle);
    activity.startActivity(intent);
  }

  @Override
  public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("BaiduFacePlugin",
            "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode + ", data=" + data);
    return false;
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {
    EventBus.getDefault().unregister(this);
  }

  @Subscribe(threadMode = ThreadMode.MAIN)
  public void onGetMessage(MessageEvent message) {
    if(message.type=="1"){
      livenessCallback.sucess(message.message);
    }else {
      detectCallback.sucess(message.message);
    }
  }

  class LivenessCallback {

    private Result result;

    public LivenessCallback(Result result) {
      this.result = result;
    }

    public void sucess(String image) {
      Map<String, String> map = new HashMap<>();
      map.put("success", "true");
      map.put("srcimage", image);
      result.success(map);
    }

    public void failed() {
      Map<String, String> map = new HashMap<>();
      map.put("success", "false");
      result.success(map);
    }

  }

  class DetectCallback {

    private Result result;

    public DetectCallback(Result result) {
      this.result = result;
    }

    public void sucess(String image) {
      Map<String, String> map = new HashMap<>();
      map.put("success", "true");
      map.put("srcimage", image);
      result.success(map);
    }

    public void failed() {
      Map<String, String> map = new HashMap<>();
      map.put("success", "false");
      result.success(map);
    }

  }
}